package com.qaml.mes.device;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName MybatisPlusGeneratorNew
 * @Description 自定生成数据库类
 * @Author leikai
 * @Date 2023/11/3 11:37
 * @Version 1.0
 */
public class MybatisPlusGeneratorNew {

    /**
     * 项目路径
     */
    public static final String PROJECT_PATH = "/device-infra";
    private static final String url = "jdbc:mysql://122.9.130.79:6033/mes_device_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false";
    private static final String username = "MES";
    private static final String password = "QAML@test";

    public static void main(String[] args) {
        List<String> tables = new ArrayList<>();
        tables.add("t_mes_holiday_apply");
        tables.add("t_mes_eqp_repair");
        tables.add("t_mes_check_upkeep_message");

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author("leikai")               //作者
                            .outputDir(System.getProperty("user.dir") + PROJECT_PATH + "\\src\\main\\java")    //输出路径(写到java目录)
                            //.enableSwagger()           //开启swagger
                            .commentDate("yyyy-MM-dd")
                            .fileOverride();            //开启覆盖之前生成的文件

                })
                .packageConfig(builder -> {
                    builder.parent("com.qaml.mes.device.config")
                            //.moduleName("mybatisplus")
                            .entity("dao")
                            .service("repository")
                            .serviceImpl("repository.impl")
                            .controller("controller")
                            .mapper("dao.mapper")
                            .xml("mapper")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + PROJECT_PATH + "\\src\\main\\resources\\mapper"));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables)
                            .addTablePrefix("t_mes_")
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .entityBuilder()
                            .formatFileName("%sDO")
                            .enableLombok()
                            .logicDeleteColumnName("isDeleted")
                            .enableTableFieldAnnotation()
                            .controllerBuilder()
                            .formatFileName("%sController")
                            .enableRestStyle()
                            .mapperBuilder()
                            .enableBaseResultMap()  //生成通用的resultMap
                            .superClass(BaseMapper.class)
                            .formatMapperFileName("%sMapper")
                            .enableMapperAnnotation()
                            .formatXmlFileName("%sMapper");
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}